
package view;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import model.PersonLid; 

 
/**
 *
 * @author 12016977 GEORGIOS FLOURIS
 */
public class LidZoeken extends javax.swing.JFrame {

    /**
     * Creates new form LidZoeken
     */
    public LidZoeken() {
        initComponents();
         setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lidZoekButton = new javax.swing.JButton();
        lidZoekField = new javax.swing.JTextField();
        wijzigButton = new javax.swing.JButton();
        verwijderButton = new javax.swing.JButton();
        searchMemberList = new components.JSqlList();
        ledenBeheerKopellingButton = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        naamColumnLabel = new javax.swing.JLabel();
        postcodeColumnLabel = new javax.swing.JLabel();
        huisnummerColumnLabel = new javax.swing.JLabel();
        StraatColumnLabel = new javax.swing.JLabel();
        codeLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("lidZoeken");

        lidZoekButton.setText("zoek");
        lidZoekButton.setToolTipText("zoekt in: code, naam, straat, postcode");
        lidZoekButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                lidZoekButtonActionPerformed(evt);
            }
        });

        wijzigButton.setText("wijzig");
        wijzigButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                wijzigButtonActionPerformed(evt);
            }
        });

        verwijderButton.setText("verwijder");
        verwijderButton.setToolTipText("een lid per keer");
        verwijderButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                verwijderButtonActionPerformed(evt);
            }
        });

        searchMemberList.setFont(new java.awt.Font("Courier New", 0, 12)); // NOI18N

        ledenBeheerKopellingButton.setText("Leden Beheer");
        ledenBeheerKopellingButton.setToolTipText("snel koppeling");
        ledenBeheerKopellingButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ledenBeheerKopellingButtonActionPerformed(evt);
            }
        });

        naamColumnLabel.setText("Naam");

        postcodeColumnLabel.setText("Postcode");

        huisnummerColumnLabel.setText("Huisnummer");

        StraatColumnLabel.setText("  Straat");

        codeLabel.setText("Code");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1)
                    .addComponent(searchMemberList, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(ledenBeheerKopellingButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(verwijderButton)
                        .addGap(15, 15, 15)
                        .addComponent(wijzigButton, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(lidZoekField)
                        .addGap(18, 18, 18)
                        .addComponent(lidZoekButton, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(codeLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(naamColumnLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 193, Short.MAX_VALUE)
                        .addComponent(postcodeColumnLabel)
                        .addGap(18, 18, 18)
                        .addComponent(huisnummerColumnLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(StraatColumnLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(194, 194, 194)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lidZoekField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lidZoekButton))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(4, 4, 4)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(naamColumnLabel)
                    .addComponent(postcodeColumnLabel)
                    .addComponent(huisnummerColumnLabel)
                    .addComponent(StraatColumnLabel)
                    .addComponent(codeLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(searchMemberList, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 21, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ledenBeheerKopellingButton)
                    .addComponent(verwijderButton)
                    .addComponent(wijzigButton, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(32, 32, 32))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    // method to get the data from the selected member inside the list
    private PersonLid getSelection() {
        PersonLid selectedLid = (PersonLid) searchMemberList.getSelectedObject();    
            if (selectedLid == null) {
                //Generic error message for not selecting a member
                JOptionPane.showMessageDialog(null, "Geen lid geselecteerd ", "error ", JOptionPane.ERROR_MESSAGE);
            }else{
                System.out.println("Getting member " + selectedLid.getNaam());
            }
        return selectedLid;
    }
    
    private void lidZoekButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_lidZoekButtonActionPerformed
        String searchTerm = lidZoekField.getText();
        //Get the members with the help of the the search term or simply list all...
        Object[] leden = PersonLid.getLeden(searchTerm);
        //show the searchMemberListhe GUI
        searchMemberList.fillList(leden);
    }//GEN-LAST:event_lidZoekButtonActionPerformed

    // Data modification
    private void wijzigButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_wijzigButtonActionPerformed
        PersonLid selectedLid = getSelection();
            if (selectedLid != null) {
                new LedenBeheer(selectedLid).setVisible(true);
                this.dispose();
            }
    }//GEN-LAST:event_wijzigButtonActionPerformed
    
    //Removing a member....
    //TO DO:
    //Member cannot be removed if there is payment pending or didnt return game.
    private void verwijderButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_verwijderButtonActionPerformed
       PersonLid selectedLid = getSelection();
       String searchTerm = lidZoekField.getText();
    // if selected member then check if the user really wants to do this operation   
    if (selectedLid != null) {
      int antwoord = JOptionPane.showConfirmDialog(null,
              "Wil je deze lid echt verwijderen?\n" + selectedLid.getNaam(),
              "verwijder lid",
              JOptionPane.YES_NO_OPTION);
      if (antwoord == JOptionPane.YES_OPTION) {
          
        if (selectedLid.delete()) {

          searchMemberList.fillList(PersonLid.getLeden(searchTerm));
          
        } else {
            
          JOptionPane.showMessageDialog(null, "Verwijderen mislukt.\nLid heeft uitleningen in de database.", "error", JOptionPane.ERROR_MESSAGE);
        
        }
      } else {
          
          //window is closed (only for the log, no message to the user is needed)
          System.out.println("lid verwijderen gecancelled ");
      }
    } else {
        //nothing is selected to perform delete action
        //System.out.println ("Geen PersonLid geselecteerd ");
    }
    }//GEN-LAST:event_verwijderButtonActionPerformed

    private void ledenBeheerKopellingButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ledenBeheerKopellingButtonActionPerformed
             new LedenBeheer().setVisible(true);
             this.dispose();
    }//GEN-LAST:event_ledenBeheerKopellingButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(LidZoeken.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(LidZoeken.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(LidZoeken.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(LidZoeken.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new LidZoeken().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel StraatColumnLabel;
    private javax.swing.JLabel codeLabel;
    private javax.swing.JLabel huisnummerColumnLabel;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JButton ledenBeheerKopellingButton;
    private javax.swing.JButton lidZoekButton;
    private javax.swing.JTextField lidZoekField;
    private javax.swing.JLabel naamColumnLabel;
    private javax.swing.JLabel postcodeColumnLabel;
    private components.JSqlList searchMemberList;
    private javax.swing.JButton verwijderButton;
    private javax.swing.JButton wijzigButton;
    // End of variables declaration//GEN-END:variables
}
